<?php
/**
 * DonReY-Control Controller
 */
class Controller_Donrey extends DRY\Application\Controller_base
{

	/**
	 * DonReY Controller entry point
	 */
	public function Execute()
	{
		$this-> dispatch_Command($this-> cmd, 'cmd_');
	}

	/**
	 * Displays main control-bar layout
	 *
	 */
	public function cmd_Load_Control()
	{
		$which_bar = $this-> controlbar?:'standard-control.html';
		echo \DRY\core::layout()-> render($which_bar, $this);
	}

	public function cmd_Load_Modules()
	{
		$modules_info = \DRY\core::control()-> config_Scan_Modules();
		foreach($modules_info as &$info)
			$info['info-json'] = json_encode($info);
		echo \DRY\core::render()-> factory_Transform('drytemplate', "donrey-modules.tpl", $modules_info, 'file');
	}

	/**
	 * Loads the configuration and operation interface for modules
	 * In frontend app, each module has it's own tab
	 */
	public function cmd_Load_Module_Interface()
	{
		$module_CFG = \DRY\core::control()-> config_Read_Alias($this-> alias);

		$config = \DRY\core::control()-> module_Command($this-> alias, "ExportConfig");
		$module_name = $module_CFG-> getName();
		echo \DRY\core::render()-> factory_Transform('drytemplate', "module-{$module_name}.tpl", $config, 'file');
	}

	public function cmd_Update_Alias()
	{
		\DRY\core::control()-> config_Update_Alias($this-> alias, $this-> request_toArray());
		header('Content-Type: application/json');
		echo 'true';
	}

	/**
	 * Module specific command.
	 * Pass this on to \DRY\core::control(), which will pass it on to the proper module (uses core to actually instance the module!)
	 */
	public function cmd_Module_Command()
	{
		header('Content-Type: application/json');
		echo json_encode(\DRY\core::control()-> module_Command($this-> alias, $this-> moduleCmd, $this-> commandData));
	}

}